Track = function()
{
  this.info = [];
  this.index;
  this.target;
}

Track.prototype.setTarget = function(dom_elem)
{
    this.target = dom_elem;
}

Track.prototype.checkLink = function(url)
{
  var track_info = this.info[this.index];
  var self = this;
  var oncompl = function(o){ 

    var r = o.responseXML.getElementsByTagName('proxy')[0].firstChild.nodeValue;

    try{
      if(r != 'OK')
      {
        mt.track.info[mt.track.index]['url'] = false;
        self.callback(url, {msg:'BAD_LINK',value:r});//
       }
       else
       {
         mt.track.info[mt.track.index]['url'] = url;
         self.callback(url);
        }
      }catch(e){ alert(e)  }
  }
  var options = {target: this.target, timeout: 0,
          onLoading:function(o, target){ mt.onLoading(target,'Verifying the link provided...'); },
          onError: function(o,target,str){ throw str; },
          onComplete: function(o,targ){oncompl(o);},        
          data: 'url='+url+'&op=link_check'};

   new Ajax().makeRequest('proxy.php', options);
}

Track.prototype.select = function(index)
{
  this.index = index;
}
Track.prototype.finished = function(track_id)
{
  this.info[this.index]['id'] = track_id;	
}

Track.prototype.add = function()
{
  var postdata = mt.serialize(this.info[this.index]);
  if(mt.article.body)
      postdata += '&article='+mt.article.body;
  
  var self = this;
  var oncomplete = function(o){
          try{ 
            var id = o.responseXML.getElementsByTagName('result')[0];

            if(id){
              self.info[self.index]['id'] = id.firstChild.nodeValue;
              self.callback(id.firstChild.nodeValue);
            }
            else{
              var errorFields =  o.responseXML.getElementsByTagName('errorFields')[0];
              if(errorFields){
               self.callback(null,{msg:"ERROR_FIELDS",value:errorFields.firstChild.nodeValue});
              }
              else throw "PROXY_ERROR";
            }
          }catch(e){
              self.callback(null,{msg:"PROXY_ERROR", value:null});
          }
        }
  var options = {target: this.target, timeout: 0, 
        onLoading: function(o,target){ mt.onLoading(target,'Adding song to our Data Base...'); },
        onError: function(o,target,str){ throw str; },
        onComplete: function(o,target){ oncomplete(o); },        
        data: postdata+'&op=add'};

 new Ajax().makeRequest('index.php?view=track&action=add', options); 
}

Track.prototype.force = function()
{
  this.info[0] = { title: $('title').getValue().trim().uword(), artist: $('artist').getValue().trim().uword(), release: null}
}

Track.prototype.fav = function(unfav,track_id){
  	var player = window.player;
  	if(player && player.track.current == null) return;
  	//else player.track_remove = unfav || false;
  	var current_track = player? player.track.current : track_id;
  	if(current_track===undefined)
  	  throw 'Could not determine the track id';
  	
    var options = {'target':unfav?$('favicon_rem_'+current_track):$('favicon_add_'+current_track), 
                  onLoading: function(o, target){	
                  	target.firstChild.style.display = 'none';
                  	mt.onLoading(target,'',1); 
                   },
                  onComplete: function(o, target)
                    {
                        target.removeChild(target.lastChild);
                        var r = o.responseXML.getElementsByTagName('result')[0];
                        if(r && r.firstChild.nodeValue == 'OK') {
                           target.style.display = 'none';
                           target.firstChild.style.display = 'inline';
                           
                           var new_target = unfav?$('favicon_add_'+current_track):$('favicon_rem_'+current_track);
                           new_target.style.display = 'inline';
                           
                           new_target.setAttribute('title',(unfav?'Song succesfully removed from your playlist. Click icon to add it to the playlist':'Song succesfully added to your playlist. Click icon to remove it from the playlist'));
                        }
                       else { alert('ERROR: '+r.firstChild.nodeValue); }
                    },
                   data:'op=fav'+(unfav?'&unfav=1&':'&')+'track_id='+current_track
                            
          };
                  
    new Ajax().makeRequest('proxy.php', options);
  }
